#
# Copyright (C) 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=wview
PKG_VERSION:=5.19.0-jgoerzen
PKG_RELEASE=$(PKG_SOURCE_VERSION)
PKG_SOURCE_URL:=git://github.com/jgoerzen/wview.git
PKG_SOURCE_VERSION:=7bfac6c11e756290c38e7b5862a4c51b6bc6c51e
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz

PKG_FIXUP:=autoreconf

include $(INCLUDE_DIR)/package.mk

define Package/wview
  SECTION:=utils
  CATEGORY:=Utilities
  DEPENDS:=+libcurl +zlib +libpng +libgd +libreadline +libsqlite3 +libusb-1.0 +radlib +sqlite3-cli
  TITLE:=Wview Weather Station Software
  URL:=http://www.wviewweather.com/
endef

define Package/wview/description
  Wview is a collection of linux/unix daemons which interface with a supported
  weather station to retrieve archive records (if generated by the station) and
  current conditions. If the station does not generate archive records
  internally, wview will auto-generate archive records based on the sensor
  readings collected for that interval. The archive records and High/Low (HILOW)
  data are stored in SQLite3 databases. At a configurable interval, wview will
  utilize the archive history and current conditions to generate weather images
  (buckets, dials and graphs) and HTML web pages based on user-configurable HTML
  templates.

  After install you need to create a directory holding archive and create the
  database there by running there
    sqlite3 wview-archive.sdb '.read /etc/wview/wview-archive.sql'

  Then copy all your theme files to the appropriate place (on a non-volatile
  storage), and html-templates.conf to /etc/wview/; then wviewconfig to configure
  the system.

  Read and amend /etc/init.d/wview appropriately for your particular setup!
endef

define Package/wview/prerm
#!/bin/sh
if [ -z "$${IPKG_INSTROOT}" ]; then
        echo "Removing rc.d symlink for wview"
        /etc/init.d/wview disable
fi
exit 0
endef

define Package/wview/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/cwop/wvcwopd $(1)/usr/bin/wvcwopd
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/http/wvhttpd $(1)/usr/bin/wvhttpd
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/procmon/wvpmond $(1)/usr/bin/wvpmond
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/utilities/hilowcreate/hilowcreate $(1)/usr/bin/hilowcreate
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/utilities/sqlite2wlk/sqlite2wlk $(1)/usr/bin/sqlite2wlk
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/htmlgenerator/htmlgend $(1)/usr/bin/htmlgend
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/VantagePro/vpconfig/vpinstall $(1)/usr/bin/vpinstall
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/ftp/wviewftpd $(1)/usr/bin/wviewftpd
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/ssh/wviewsshd $(1)/usr/bin/wviewsshd
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/Virtual/wviewd_virtual $(1)/usr/bin/wviewd_virtual
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/utilities/archive-be2le/arc_be2le $(1)/usr/bin/arc_be2le
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/utilities/archive-le2be/arc_le2be $(1)/usr/bin/arc_le2be
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/alarms/wvalarmd $(1)/usr/bin/wvalarmd
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/utilities/wlk2sqlite/wlk2sqlite $(1)/usr/bin/wlk2sqlite
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/Simulator/wviewd_sim $(1)/usr/bin/wviewd_sim
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/TWI/wviewd_twi $(1)/usr/bin/wviewd_twi
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/VantagePro/wviewd_vpro $(1)/usr/bin/wviewd_vpro
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/wviewconfig/wviewconfig $(1)/usr/bin/wviewconfig
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/wviewconfig/wviewhtmlconfig $(1)/usr/bin/wviewhtmlconfig
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/WH1080/wviewd_wh1080 $(1)/usr/bin/wviewd_wh1080
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/WMR918/wviewd_wmr918 $(1)/usr/bin/wviewd_wmr918
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/WS-2300/wviewd_ws2300 $(1)/usr/bin/wviewd_ws2300
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/WMRUSB/wviewd_wmrusb $(1)/usr/bin/wviewd_wmrusb
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/WXT510/wviewd_wxt510 $(1)/usr/bin/wviewd_wxt510
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/VantagePro/vpconfig/vpconfig $(1)/usr/bin/vpconfig
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/wviewconfig/wviewcleardata $(1)/usr/bin/wviewcleardata
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/TE923/wviewd_te923 $(1)/usr/bin/wviewd_te923
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations/WXT510/wxt510config/wxt510config $(1)/usr/bin/wxt510config
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/etc/init.d/wview $(1)/etc/init.d/
	$(INSTALL_DIR) $(1)/etc/wview
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/conf/wview-conf.sql $(1)/etc/wview/wview-conf.sql
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/graphics.conf $(1)/etc/wview/graphics.conf
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/post-generate.sh $(1)/etc/wview/post-generate.sh
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/images.conf $(1)/etc/wview/images.conf
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/images-user.conf $(1)/etc/wview/images-user.conf
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/conf/arcrec-header.conf $(1)/etc/wview/arcrec-header.conf
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/pre-generate.sh $(1)/etc/wview/pre-generate.sh
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/conf/wview-conf-update.sql $(1)/etc/wview/wview-conf-update.sql
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/conf/forecast.conf-no-forecast $(1)/etc/wview/forecast.conf-no-forecast
	echo $(PKG_VERSION) > $(1)/etc/wview/wview-version
	echo wviewd_sim > $(1)/etc/wview/wview-binary

# This should never be defined as the user is expected to manually install all the
# theme files he wants to an appropriate place and to specify that place in the config.
# Leaving here for the future reference to be able to prepare a full variant of the package
ifdef INSTALL_TEMPLATES
	$(INSTALL_DIR) $(1)/etc/wview/html
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/Template-Skins-HOWTO.txt $(1)/etc/wview/html/Template-Skins-HOWTO.txt
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/parameterlist.htx $(1)/etc/wview/html/parameterlist.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/parameterlist.txt $(1)/etc/wview/html/parameterlist.txt
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/awekas_wl.htx-metric $(1)/etc/wview/html/awekas_wl.htx-metric
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/Current.htx $(1)/etc/wview/html/Current.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/html-templates.conf $(1)/etc/wview/html/html-templates.conf
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/Weekly.htx $(1)/etc/wview/html/Weekly.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/Monthly.htx $(1)/etc/wview/html/Monthly.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/data.htx $(1)/etc/wview/html/data.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/index-day.htx $(1)/etc/wview/html/index-day.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/Yearly.htx $(1)/etc/wview/html/Yearly.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/Daily.htx $(1)/etc/wview/html/Daily.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/almanac.htx $(1)/etc/wview/html/almanac.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/header-big.incx $(1)/etc/wview/html/header-big.incx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/index.htx $(1)/etc/wview/html/index.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/awekas_wl.htx-us $(1)/etc/wview/html/awekas_wl.htx-us
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/Current_Conditions.htx $(1)/etc/wview/html/Current_Conditions.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/header.incx $(1)/etc/wview/html/header.incx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/index-night.htx $(1)/etc/wview/html/index-night.htx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/wxrss.xtx $(1)/etc/wview/html/wxrss.xtx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/readings.incx $(1)/etc/wview/html/readings.incx
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/standard/nav-buttons.incx $(1)/etc/wview/html/nav-buttons.incx
	$(INSTALL_DIR) $(1)/etc/wview/alarms
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/examples/alarms/outtempMax.sh $(1)/etc/wview/alarms/outtempMax.sh
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/examples/alarms/outtempMin.sh $(1)/etc/wview/alarms/outtempMin.sh
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/examples/alarms/outtempMaxEmail.sh $(1)/etc/wview/alarms/outtempMaxEmail.sh
	$(INSTALL_DIR) $(1)/var/run/wview
	$(INSTALL_DIR) $(1)/var/wview/img
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/fc-icon-partlycloudy.gif $(1)/var/wview/img/fc-icon-partlycloudy.gif
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/fc-icon-rain.gif $(1)/var/wview/img/fc-icon-rain.gif
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/chart_bg.png $(1)/var/wview/img/chart_bg.png
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/dot_bg.png $(1)/var/wview/img/dot_bg.png
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/fc-icon-snowandrain.gif $(1)/var/wview/img/fc-icon-snowandrain.gif
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/Clouds.jpg $(1)/var/wview/img/Clouds.jpg
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/Clouds-blue.jpg $(1)/var/wview/img/Clouds-blue.jpg
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/fc-icon-partlycloudyandsnow.gif $(1)/var/wview/img/fc-icon-partlycloudyandsnow.gif
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/dial_bg.png $(1)/var/wview/img/dial_bg.png
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/fc-icon-snow.gif $(1)/var/wview/img/fc-icon-snow.gif
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/bucket_bg.png $(1)/var/wview/img/bucket_bg.png
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/fc-icon-cloudy.gif $(1)/var/wview/img/fc-icon-cloudy.gif
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/django_bg.jpg $(1)/var/wview/img/django_bg.jpg
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/fc-icon-sunny.gif $(1)/var/wview/img/fc-icon-sunny.gif
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/fc-icon-partlycloudyandrain.gif $(1)/var/wview/img/fc-icon-partlycloudyandrain.gif
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/examples/html/chrome/static/above-clouds.jpg $(1)/var/wview/img/above-clouds.jpg

# These files and the directories should be created manually wherever the user is going to
# have her weather archive and the generated html files
	$(INSTALL_DIR) $(1)/var/wview/img/NOAA
	$(INSTALL_DIR) $(1)/var/wview/img/Archive
	$(INSTALL_DIR) $(1)/var/wview/noaa
	$(INSTALL_DIR) $(1)/var/wview/alarms
	$(INSTALL_DIR) $(1)/var/wview/archive
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/bin/archive/wview-archive.sql $(1)/var/wview/archive/wview-archive.sql
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/bin/archive/readme.txt $(1)/var/wview/archive/readme.txt
endif
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/bin/archive/wview-archive.sql $(1)/etc/wview/wview-archive.sql
endef

$(eval $(call BuildPackage,wview))

